﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TheVip
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        bool kiemtra = false;
        bool checkClose = false;
        int sodiemthuong = 0;
        int diemdadung = 0;
        int tongdiem = 0;
        int sodiemthe = 0;
        string madiemthuong;
        Entities.TheVip temp;
        Entities.TheVip thevip;
        Entities.DiemThuongKhachHang[] diemthuongkhachhang;
        Entities.DiemThuongKhachHang diemthuong;
        Entities.LoaiTheVip[] loaithevip;
        #endregion

        #region khởi tạo
        public frmXuLy()
        {
            InitializeComponent();
            try
            {
                LoaiTheVip();
                diemthuongkhachhang = Common.StaticVariable.InterfaceObject.SelectTinhDiemThuong();
                txtMaTheVip.Text = getID();
            }
            catch
            {
            }
        }

        public frmXuLy(Entities.TheVip thevip)
        {
            InitializeComponent();
            try
            {
                btnGhiLai.Enabled = false;
                temp = thevip;
                XuLyDuLieu(thevip);
                kiemtra = true;
                LoaiTheVip();
                diemthuongkhachhang = Common.StaticVariable.InterfaceObject.SelectTinhDiemThuong();
                txtMaTheVip.Text = getID();
            }
            catch
            {
            }
        }

        public frmXuLy(Entities.DiemThuongKhachHang diemthuongkhachhang)
        {
            InitializeComponent();
            try
            {
                LoaiTheVip();
                this.diemthuongkhachhang = Common.StaticVariable.InterfaceObject.SelectTinhDiemThuong();
                txtMaTheVip.Text = getID();
                txtMaKhachHang.Text = diemthuongkhachhang.MaKhachHang;
                txtTenKhachHang.Text = diemthuongkhachhang.TenKhachHang;
                txtMaKhachHang.ReadOnly = txtMaKhachHang.ReadOnly = true;
                SoDiemThuong();
            }
            catch
            {
            }
        }

        #endregion

        #region thêm khách hàng
        void ThemKhachHang()
        {
            try
            {
                TimKiem.frmKhachHang frm = new TimKiem.frmKhachHang();
                frm.ShowDialog();
                // chechk object
                if (TimKiem.frmKhachHang.khachhang == null)
                {
                    return;
                }
                // gán giá trị
                txtMaKhachHang.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                txtTenKhachHang.Text = TimKiem.frmKhachHang.khachhang.Ten;
                TimKiem.frmKhachHang.khachhang = null;
                cbbLoaiThe.Focus();
                SoDiemThuong();

            }
            catch
            {
            }
        }
        #endregion

        #region Ghi Lại
        void GhiLai()
        {
            try
            {
                if (!btnGhiLai.Enabled)
                {
                    return;
                }
                thevip = XuLyObject(Common.Constants.Insert);
                // check object
                if (thevip == null)
                {
                    return;
                }
                // check conflic insert
                if (!CheckConflicInsert(thevip))
                {
                    MessageBox.Show(Common.MSG.MSG5(thevip.MaTheVip), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    GetData(true);
                    txtMaTheVip.Text = mathevip;
                    return;
                }
                // check điểm thẻ
                if (sodiemthuong < sodiemthe)
                {
                    MessageBox.Show("Số điểm thưởng của khách hàng không đủ", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check insert thẻ vip
                if (Common.StaticVariable.InterfaceObject.InsertUpdateTheVip(thevip) == 0)
                {
                    return;
                }
                // check object
                XuLy(Common.Constants.Update);
                if (diemthuong == null)
                {
                    return;
                }
                // check update điểm thưởng khách hàng
                if (Common.StaticVariable.InterfaceObject.InsertUpdateTinhDiemThuong(diemthuong) == 0)
                {
                    return;
                }
                checkClose = true;
                this.Close();

            }
            catch
            {
            }
        }

        public Entities.DiemThuongKhachHang XuLy(string hanhDong)
        {
            try
            {
                diemthuong = new Entities.DiemThuongKhachHang();
                diemthuong.DiemThuongKhachHangID = 0;
                diemthuong.Deleted = false;
                diemthuong.HanhDong = hanhDong;
                diemthuong.GhiChu = "";
                diemthuong.MaDiemThuongKhachHang = madiemthuong;
                diemthuong.DiemDaDung = (diemdadung + sodiemthe).ToString();
                diemthuong.TongDiem = tongdiem.ToString();
                diemthuong.MaKhachHang = txtMaKhachHang.Text;
                diemthuong.TenKhachHang = txtTenKhachHang.Text;
                return diemthuong;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region Check Conflic Insert
        string mathevip;
        public bool CheckConflicInsert(Entities.TheVip thevip)
        {
            mathevip = getID();
            if (thevip.MaTheVip == mathevip)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý id
        public string getID()
        {
            try
            {
                string str = "";
                Entities.GetID layid = new Entities.GetID();
                layid.TableName = "TheVip";
                Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);
                if (lay.ColumnName == null)
                {
                    return "TV_0001";
                }
                str = Common.Utilities.ProcessID(lay.ColumnName);
                return str;
            }
            catch
            {
                return "TV_0001";
            }
        }
        #endregion

        #region loại the vip
        void LoaiTheVip()
        {
            loaithevip = Common.StaticVariable.InterfaceObject.SelectLoaiTheVip("1");

            Common.Utilities.XuLyComboBox(cbbLoaiThe, loaithevip, Common.Constants.LoaiTheVip.TenLoaiThe, Common.Constants.LoaiTheVip.MaLoaiTheVip);
            if (loaithevip.Length != 0)
            {
                cbbLoaiThe.SelectedIndex = 0;
                txtGiaTriConLai.Text = loaithevip[0].GiaTriThe;
                sodiemthe = int.Parse(loaithevip[0].SoDiemThuong);
            }
        }

        string TenLoaiThe(string maLoaiThe)
        {
            try
            {
                foreach (Entities.LoaiTheVip item in loaithevip)
                {
                    if (item.MaLoaiTheVip == maLoaiThe)
                    {
                        return item.TenLoaiThe;
                    }
                }
                return "";
            }
            catch
            {
                return "";
            }
        }

        string GiaTriThe(string tenLoaiThe)
        {
            try
            {
                foreach (Entities.LoaiTheVip item in loaithevip)
                {
                    if (item.TenLoaiThe == tenLoaiThe)
                    {
                        return item.GiaTriThe;
                    }
                }
                return "";
            }
            catch
            {
                return "";
            }
        }

        int SoDiemThuong(string tenLoaiThe)
        {
            try
            {
                foreach (Entities.LoaiTheVip item in loaithevip)
                {
                    if (item.TenLoaiThe == tenLoaiThe)
                    {
                        return int.Parse(item.SoDiemThuong);
                    }
                }
                return 0;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region validate
        public bool Validate()
        {
            try
            {
                if (txtMaKhachHang.Text == "<F4 - Tra cứu>")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbMaKhachHang.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtMaKhachHang.Focus();
                    return false;
                }
                if (cbbLoaiThe.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbLoaiThe.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbLoaiThe.Focus();
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        private void cbbLoaiThe_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                txtGiaTriConLai.Text = GiaTriThe(cbbLoaiThe.Text);
                sodiemthe = SoDiemThuong(cbbLoaiThe.Text);
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý object
        public void XuLyDuLieu(Entities.TheVip thevip)
        {
            try
            {
                txtGhiChu.Text = thevip.GhiChu;
                txtGiaTriConLai.Text = thevip.GiaTriConLai;
                cbbLoaiThe.Text = TenLoaiThe(thevip.LoaiThe);
                txtMaKhachHang.Text = thevip.MaKhachHang;
                txtMaTheVip.Text = thevip.MaTheVip;
                txtTenKhachHang.Text = thevip.TenKhachHang;
            }
            catch
            {
            }
        }

        // lấy obj
        public Entities.TheVip XuLyObject(string hanhDong)
        {
            try
            {
                if (Validate())
                {
                    thevip = new Entities.TheVip();
                    thevip.Deleted = false;
                    thevip.GhiChu = txtGhiChu.Text;
                    thevip.GiaTriConLai = txtGiaTriConLai.Text;
                    thevip.HanhDong = hanhDong;
                    thevip.LoaiThe = ((Entities.LoaiTheVip)cbbLoaiThe.SelectedItem).MaLoaiTheVip;
                    thevip.MaKhachHang = txtMaKhachHang.Text;
                    thevip.MaTheVip = txtMaTheVip.Text;
                    thevip.TenKhachHang = txtTenKhachHang.Text;
                    thevip.TaiKhoan = UtilitiesVariable.User;
                    return thevip;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region xử lý tính điểm thưởng
        void SoDiemThuong()
        {
            try
            {
                foreach (Entities.DiemThuongKhachHang item in diemthuongkhachhang)
                {
                    if (item.MaKhachHang == txtMaKhachHang.Text)
                    {
                        madiemthuong = item.MaDiemThuongKhachHang;
                        diemdadung = int.Parse(item.DiemDaDung);
                        tongdiem = int.Parse(item.TongDiem);
                        sodiemthuong = tongdiem - diemdadung;
                        return;
                    }
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Event
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            btnGhiLai.Click += new EventHandler(btnGhiLai_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            txtMaKhachHang.KeyUp += new KeyEventHandler(txtMaKhachHang_KeyUp);
            cbbLoaiThe.SelectedIndexChanged += new EventHandler(cbbLoaiThe_SelectedIndexChanged);
            this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
            txtMaKhachHang.Focus();
            
        }

        void cbbLoaiThe_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                txtGiaTriConLai.Text = GiaTriThe(cbbLoaiThe.Text);
                sodiemthe = SoDiemThuong(cbbLoaiThe.Text);
            }
            catch
            {
            }
        }

        void txtMaKhachHang_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F4)
            {
                ThemKhachHang();
            }
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnGhiLai_Click(object sender, EventArgs e)
        {
            GhiLai();
        }
        #endregion
    }
}
#endregion